<template>
  <!-- 个人考核维护 -->
  <base-breadcrumb :title="$route.meta.title">
    <!-- 搜索 -->
    <searchData :searchData="searchArr" @search="search"></searchData>
    <!-- 表格 -->
    <a-card :bordered="false">
      <!-- <title-name title="安全责任指标考核" /> -->
      <a-button
        v-if="loading"
        class="mb12"
        type="primary"
        :disabled="isAdd()"
        @click="handleManage(2)"
        v-btnPermission="'hse_016_add_01'"
        >维护自己的工作项</a-button
      >
      <span class="ml12" style="color: red"
        >注：个人考核项维护，每月7号截止。7号以后，不可再维护提交当月的考核项。</span
      >
      <!-- 表格 -->
      <list-table
        ref="table"
        :page-keys="['pageNo', 'pageSize', 'totalSize']"
        tableLayout="fixed"
        size="default"
        rowKey="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
        showPagination="auto"
      >
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a @click="handleManage(0, record)" v-btnPermission="'hse_016_loo_04'">查看</a>
            <span
              v-if="record.auditStatus != 'SUBMITTED' && record.auditStatus != 'PAST'"
              v-btnPermission="'hse_016_edi_02'"
            >
              <a-divider type="vertical" />
              <a @click="handleManage(1, record)">编辑</a>
            </span>

            <!-- 删除 -->
            <span
              v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'"
              v-btnPermission="'hse_016_del_03'"
            >
              <a-divider type="vertical"></a-divider>
              <a @click="handleRemoveParams({ id: record.id })">删除</a>
            </span>

            <span
              v-if="
                record.auditStatus === 'PAST' ||
                record.auditStatus === 'SUBMITTED' ||
                record.auditStatus === 'REJECTED' ||
                record.auditStatus === 'ABOLISH'
              "
              v-btnPermission="'hse_016_loo_04'"
            >
              <a-divider type="vertical" />
              <approval-button :id="record.id" :status="record.auditStatus" />
            </span>
          </MoreButton>
        </template>
        <template slot="auditStatus" slot-scope="text, record">
          <list-status :status="record.auditStatus" conversion="en" :is-approval="true" :text="text" />
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格

import BasePage from '@/views/basePage' // 基础配置
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import api from '@/api/safetySupervision/base/inspectionTemplate.js'
import MoreButton from '@/components/MoreButton'
import Moment from 'moment'
const searchArr = [
  // 搜索框
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   valueKey: 'branchCompanyId',
  //   type: SelectSubCompany
  // },
  // {
  //   name: '所属项目',
  //   keyValue: 'branchCompanyId',
  //   valueKey: 'projectId',
  //   type: SelectProject
  // },

  {
    name: '部门',
    type: 'input',
    value: 'checkDept'
  },
  {
    name: '岗位',
    type: 'input',
    value: 'checkPost'
  },
  {
    name: '姓名',
    type: 'input',
    value: 'checkName'
  }
]
const columns = [
  {
    title: '考核月份',
    dataIndex: 'checkMonth',
    width: 100
  },

  {
    title: '姓名',
    dataIndex: 'creatorName',
    width: 150,
    ellipsis: true
  },
  {
    title: '岗位',
    dataIndex: 'checkPost',
    width: 100,
    ellipsis: true
  },
  {
    title: '部门',
    dataIndex: 'checkDept',
    width: 100,
    ellipsis: true
  },
  {
    title: '所属分公司',
    dataIndex: 'branchCompanyName',
    width: 150,
    ellipsis: true
  },
  {
    title: '所属项目',
    dataIndex: 'projectName',
    width: 150,
    ellipsis: true
  },

  {
    title: '状态',
    dataIndex: 'auditStatus',
    width: 150,
    scopedSlots: { customRender: 'auditStatus' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 180,
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable,
    MoreButton
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      queryParam: {}, // 查询参数
      list: [],
      loading: false,
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        console.log(requestParameters)
        return api.listPerson(requestParameters).then((res) => {
          this.list = res.data
          this.loading = true
          return res
        })
      },
      removeApi: { remove: api.delPerson } // api
    }
  },
  methods: {
    //   本月还未提交考核项维护，并已经超过7号，则点击 维护自己的工作项 ，添加的是下月考核项
    // 本月已提交考核项维护，审核中或者审核不通过 ，则点击 维护自己的工作项 置灰，不可点击。
    // 本月已提交考核项维护，并审核通过，则 维护自己的工作项 可点击，点击添加的是下月的考核项。
    // 下月的考核项已提交，则点击 维护自己的工作项 置灰，不可点击添加后面月份的考核项。
    isAdd() {
      let flag = false
      console.log(this.list)
      this.list.forEach((e, i) => {
        if (
          e.auditStatus != 'PAST' &&
          e.checkMonth == Moment().format('YYYY-MM') &&
          e.creatorId == this.$userInfo().id
        ) {
          flag = true
        }
        if (Moment().add(1, 'month').format('YYYY-MM') == e.checkMonth && e.creatorId == this.$userInfo().id) {
          flag = true
        }
      })
      return flag
    },
    // 搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'updatePersonMaintenance',
        query
      })
    }
  }
}
</script>
